package sdkshell.kuaiyan.com.kuaiyansdk.manager;

import android.content.Context;

import com.wmcsk.KuaiyanSdk;
import com.wmcsk.dl.util.LogUtils;

import org.json.JSONObject;

import java.io.File;

import sdkshell.kuaiyan.com.kuaiyansdk.BuildConfig;
import sdkshell.kuaiyan.com.kuaiyansdk.SdkInit;
import sdkshell.kuaiyan.com.kuaiyansdk.bean.ApkNames;
import sdkshell.kuaiyan.com.kuaiyansdk.http.Ihttp.NetBuilderConfig;
import sdkshell.kuaiyan.com.kuaiyansdk.http.Net;
import sdkshell.kuaiyan.com.kuaiyansdk.http.NetApi;
import sdkshell.kuaiyan.com.kuaiyansdk.http.callback.DownloadCallBack;
import sdkshell.kuaiyan.com.kuaiyansdk.http.enums.RequestType;
import sdkshell.kuaiyan.com.kuaiyansdk.util.FileUtil;


/**
 * Created by wm on 2017/7/10.
 */
public class NetWorkPluginManager {
    private static NetWorkPluginManager netWorkPluginManager;
    private Context context;
    private NetWorkPluginManager(Context context){
        this.context = context;
    }
    public static NetWorkPluginManager getInstance(Context context){
        if(netWorkPluginManager == null){
            synchronized (NetWorkPluginManager.class){
                if(netWorkPluginManager == null){
                    netWorkPluginManager = new NetWorkPluginManager(context);
                }
            }
        }

        return netWorkPluginManager;
    }


    public void checkUpdate(final String apkName){
        double version = LocalPluginMager.getInstance().getLocalPluginBean(apkName).pluginVersion;
        int shellVersion = LocalPluginMager.getInstance().getLocalPluginBean(apkName).sdkVersion;
        if(BuildConfig.isTest){
            LogUtils.getNewInstance(KuaiyanSdk.ERROR_TAG).eob("主工程版本"+shellVersion+"  插件版本"+version);
        }
        Net net = NetApi.getNetProxyInstance(context);
        NetBuilderConfig config = net.getConfig();
        config.getNetConfig().setUrl(ApkNames.getPluginUrl(apkName)).addParams("sdkVersion",shellVersion+"").addParams("pluginVersion",version+"").setType(RequestType.GET).setEncoding("utf-8");
        config.getExceptionConfig().setShowGsonExcetionToast(false).toastForNoNetwork(false).toastIoForException(false).toastIoForNoWifyException(false);
        net.init(config);
        net.requestNet_info(new DownloadCallBack(){
            @Override
            public void onError(Exception e, String response) {
                super.onError(e, response);
            }
            @Override
            public void onResponse(Object response, String json) {
                super.onResponse(response, json);
                if(BuildConfig.isTest){
                    LogUtils.getNewInstance(KuaiyanSdk.ERROR_TAG).eob("请求结果"+json);
                }
                handleCheckInfo(json,apkName);
            }
        });
    }

    public void handleCheckInfo(String json, String apkName){
        try {
            JSONObject jsonObject = new JSONObject(json);
            String code = jsonObject.optString("code");
            if("1".equals(code)){
                String data = jsonObject.optString("data");
                if(data!=null){
                    downloadPlugin(data,apkName);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 下载
     */
    public void downloadPlugin(String downloadPath, final String apkName){
        Net net = NetApi.getNetProxyInstance(SdkInit.getContext());
        NetBuilderConfig config = net.getConfig();
        config.getNetConfig().setUrl(downloadPath).setType(RequestType.GET).setIsDownload(true).setDownloadPath(new File(FileUtil.getDownCacheDir(context,FileUtil.NETWORK_PATH),apkName+".temp").getAbsolutePath());
        config.getExceptionConfig().setShowGsonExcetionToast(false).toastForNoNetwork(false).toastIoForException(false).toastIoForNoWifyException(false);
        net.init(config);
        net.requestNet_info(new DownloadCallBack(){
            @Override
            public void downloadOk(String downloadPath) {
                super.downloadOk(downloadPath);
                File file = new File(downloadPath);
                file.renameTo(new File(FileUtil.getDownCacheDir(SdkInit.getContext(), FileUtil.NETWORK_PATH).getAbsolutePath(), apkName));
            }
            @Override
            public void onError(Exception e, String response) {
                super.onError(e, response);
                e.printStackTrace();
            }
        });
    }
}
